{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]14891/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","excerpt":"문제 총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴가 1번, 그 오른쪽은 2번, 그 오른쪽은 3번, 가장 오른쪽 톱니바퀴는 4번이다. 이때, 톱니바퀴를 총 K번 회전시키려고 한다. 톱니바퀴의 회전은 한 칸을…","html":"<h2>문제</h2>\n<p>총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴가 1번, 그 오른쪽은 2번, 그 오른쪽은 3번, 가장 오른쪽 톱니바퀴는 4번이다.</p>\n<p>이때, 톱니바퀴를 총 K번 회전시키려고 한다. 톱니바퀴의 회전은 한 칸을 기준으로 한다. 회전은 시계 방향과 반시계 방향이 있다.</p>\n<p>톱니바퀴를 회전시키려면, 회전시킬 톱니바퀴와 회전시킬 방향을 결정해야 한다. 톱니바퀴가 회전할 때, 서로 맞닿은 극에 따라서 옆에 있는 톱니바퀴를 회전시킬 수도 있고, 회전시키지 않을 수도 있다. 톱니바퀴 A를 회전할 때, 그 옆에 있는 톱니바퀴 B와 서로 맞닿은 톱니의 극이 다르다면, B는 A가 회전한 방향과 반대방향으로 회전하게 된다. 예를 들어, 아래와 같은 경우를 살펴보자.</p>\n<img src='https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/14891/4.png' width='80%'>\n<p>두 톱니바퀴의 맞닿은 부분은 초록색 점선으로 묶여있는 부분이다. 여기서, 3번 톱니바퀴를 반시계 방향으로 회전했다면, 4번 톱니바퀴는 시계 방향으로 회전하게 된다. 2번 톱니바퀴는 맞닿은 부분이 S극으로 서로 같기 때문에, 회전하지 않게 되고, 1번 톱니바퀴는 2번이 회전하지 않았기 때문에, 회전하지 않게 된다. 따라서, 아래 그림과 같은 모양을 만들게 된다.</p>\n<img src='https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/14891/5.png' width='80%'>\n<p>1번 톱니바퀴를 시계 방향으로 회전시키면, 2번 톱니바퀴가 반시계 방향으로 회전하게 되고, 2번이 회전하기 때문에, 3번도 동시에 시계 방향으로 회전하게 된다. 4번은 3번이 회전하지만, 맞닿은 극이 같기 때문에 회전하지 않는다. 따라서, 아래와 같은 상태가 된다.</p>\n<img src='https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/14891/6.png' width='80%'>\n<p>톱니바퀴의 초기 상태와 톱니바퀴를 회전시킨 방법이 주어졌을 때, 최종 톱니바퀴의 상태를 구하는 프로그램을 작성하시오.</p>\n<h2>입, 출력</h2>\n<p>입력</p>\n<ul>\n<li>첫째 줄에 1번 톱니바퀴의 상태, 둘째 줄에 2번 톱니바퀴의 상태, 셋째 줄에 3번 톱니바퀴의 상태, 넷째 줄에 4번 톱니바퀴의 상태가 주어진다. 상태는 8개의 정수로 이루어져 있고, 12시방향부터 시계방향 순서대로 주어진다. N극은 0, S극은 1로 나타나있다.</li>\n<li>다섯째 줄에는 회전 횟수 K(1 ≤ K ≤ 100)가 주어진다. 다음 K개 줄에는 회전시킨 방법이 순서대로 주어진다. 각 방법은 두 개의 정수로 이루어져 있고, 첫 번째 정수는 회전시킨 톱니바퀴의 번호, 두 번째 정수는 방향이다. 방향이 1인 경우는 시계 방향이고, -1인 경우는 반시계 방향이다.</li>\n</ul>\n<p>출력</p>\n<ul>\n<li>\n<p>총 K번 회전시킨 이후에 네 톱니바퀴의 점수의 합을 출력한다. 점수란 다음과 같이 계산한다.</p>\n<ul>\n<li>1번 톱니바퀴의 12시방향이 N극이면 0점, S극이면 1점</li>\n<li>2번 톱니바퀴의 12시방향이 N극이면 0점, S극이면 2점</li>\n<li>3번 톱니바퀴의 12시방향이 N극이면 0점, S극이면 4점</li>\n<li>4번 톱니바퀴의 12시방향이 N극이면 0점, S극이면 8점</li>\n</ul>\n</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Gear {\n    int[] tooth;\n    boolean spinStatus;\n    int spinDir;\n\n    public Gear() {\n        tooth = new int[8];\n        spinStatus = false;\n        spinDir = 0;\n    }\n\n    public void clockWise() {\n        int tmp = tooth[7];\n        System.arraycopy(tooth, 0, tooth, 1, 7);\n        tooth[0] = tmp;\n    }\n\n    public void counterClockWise() {\n        int tmp = tooth[0];\n        System.arraycopy(tooth, 1, tooth, 0, 7);\n        tooth[7] = tmp;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>톱니바퀴의 정보를 저장하는 클래스이다.</li>\n<li><code class=\"language-text\">clockWise()</code> 는 톱니를 시계방향으로 돌렸을 때 상태 업데이트를 수행하고, <code class=\"language-text\">counterClockWise()</code> 는 반시계방향에 대한 업데이트를 수행한다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">for (int i = 0; i &lt; N; i++) {\n    spins[i] = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n    int centerG = spins[i][0] - 1;\n    gears[centerG].spinStatus = true;\n    gears[centerG].spinDir = spins[i][1];\n\n    for (int j = centerG - 1; j &gt;= 0; j--) {\n        if (gears[j + 1].spinStatus) {\n            if (gears[j].tooth[2] != gears[j + 1].tooth[6]) {\n                gears[j].spinStatus = true;\n                gears[j].spinDir = gears[j + 1].spinDir * -1;\n            }\n        }\n    }\n\n    for (int j = centerG + 1; j &lt; 4; j++) {\n        if (gears[j - 1].spinStatus) {\n            if (gears[j].tooth[6] != gears[j - 1].tooth[2]) {\n                gears[j].spinStatus = true;\n                gears[j].spinDir = gears[j - 1].spinDir * -1;\n            }\n        }\n    }\n\n    for (int j = 0; j &lt; 4; j++) {\n        if (gears[j].spinDir == -1) gears[j].counterClockWise();\n        else if (gears[j].spinDir == 1) gears[j].clockWise();\n        gears[j].spinDir = 0;\n        gears[j].spinStatus = false;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>\n<p>중심 구현부는 크게 3개로 나눌 수 있다.</p>\n<ul>\n<li>회전하는 톱니의 왼쪽 톱니에 대해 회전여부를 판단하고 수행하는 for문</li>\n<li>회전하는 톱니의 오른쪽 톱니에 대해 회전여부를 판단하고 수행하는 for문</li>\n<li>주어진 1회전이 끝난 후 톱니의 정보를 업데이트하는 for문</li>\n</ul>\n</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.*;\n\nclass Gear {\n    int[] tooth;\n    boolean spinStatus;\n    int spinDir;\n\n    public Gear() {\n        tooth = new int[8];\n        spinStatus = false;\n        spinDir = 0;\n    }\n\n    public void clockWise() {\n        int tmp = tooth[7];\n        System.arraycopy(tooth, 0, tooth, 1, 7);\n        tooth[0] = tmp;\n    }\n\n    public void counterClockWise() {\n        int tmp = tooth[0];\n        System.arraycopy(tooth, 1, tooth, 0, 7);\n        tooth[7] = tmp;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        Gear[] gears = new Gear[4];\n\n        for (int i = 0; i &lt; 4; i++) {\n            gears[i] = new Gear();\n            gears[i].tooth = Arrays.stream(br.readLine().split(&quot;&quot;)).mapToInt(Integer::parseInt).toArray();\n        }\n\n        int N = Integer.parseInt(br.readLine());\n        int[][] spins = new int[N][2];\n\n        for (int i = 0; i &lt; N; i++) {\n            spins[i] = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n            int centerG = spins[i][0] - 1;\n            gears[centerG].spinStatus = true;\n            gears[centerG].spinDir = spins[i][1];\n\n            for (int j = centerG - 1; j &gt;= 0; j--) {\n                if (gears[j + 1].spinStatus) {\n                    if (gears[j].tooth[2] != gears[j + 1].tooth[6]) {\n                        gears[j].spinStatus = true;\n                        gears[j].spinDir = gears[j + 1].spinDir * -1;\n                    }\n                }\n            }\n\n            for (int j = centerG + 1; j &lt; 4; j++) {\n                if (gears[j - 1].spinStatus) {\n                    if (gears[j].tooth[6] != gears[j - 1].tooth[2]) {\n                        gears[j].spinStatus = true;\n                        gears[j].spinDir = gears[j - 1].spinDir * -1;\n                    }\n                }\n            }\n\n            for (int j = 0; j &lt; 4; j++) {\n                if (gears[j].spinDir == -1) gears[j].counterClockWise();\n                else if (gears[j].spinDir == 1) gears[j].clockWise();\n                gears[j].spinDir = 0;\n                gears[j].spinStatus = false;\n            }\n        }\n\n        N = 0;\n        for (int i = 0; i &lt; 4; i++) N += gears[i].tooth[0] * Math.pow(2,i);\n\n        System.out.println(N);\n\n        br.close();\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>정말 오랜만에 1트만에 성공했다. 난이도 높은 문제는 아니었지만 ...</li>\n<li>문제 풀이를 구상하면서, 어떤 메소드를 구현해야하며, 어떤 메소드를 객체에 둬야 하는지에 대해 좀 더 공부해봐야겠다.</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/14891\">https://www.acmicpc.net/problem/14891</a></p>","frontmatter":{"title":"👩‍💻 14891. 톱니바퀴","date":"March 06, 2022","update":"March 06, 2022","tags":["구현","시뮬레이션"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]14891/","readingTime":{"minutes":8.52}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[JPA]2/"},"frontmatter":{"title":"📚 2. JPA 시작"}},"next":{"fields":{"slug":"/[JPA]3/"},"frontmatter":{"title":"📚 3. 영속성 관리"}}},"pageContext":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","series":"👩‍💻 BOJ","previousPostId":"f86fa867-f096-52ce-854a-ef00e49ebcdb","nextPostId":"ffb8392d-d300-51c1-8e71-a8943ef4b58d"}},"staticQueryHashes":[]}